package com.flyou.ccnucenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.GetMapList;
import com.flyou.ccnucenter.ui.activity.henuBeside.map.MapActivity;
import com.flyou.ccnucenter.widget.FousedTextView;

import java.util.List;

/**
 * ============================================================
 * 项目名称：HenuCenterPro
 * 包名称：com.flyou.henucenter.adapter
 * 文件名：NoteRecycleViewAdapter
 * 类描述：
 * 创建人：flyou
 * 邮箱：fangjaylong@gmail.com
 * 创建时间：2015/12/26 15:03
 * 修改备注：
 * 版本：@version  V1.0
 * ============================================================
 **/
public class MapRecycleViewAdapter extends RecyclerView.Adapter<MapRecycleViewAdapter.MyViewHolder> {
    private Context context;
    private List<GetMapList.PointListEntity> points;
    private OnItemClickLitener mOnItemClickLitener;
    private String keyWord;

    public MapRecycleViewAdapter(Context context, List<GetMapList.PointListEntity> points, String keyWord) {
        this.context = context;
        this.points = points;
        this.keyWord = keyWord;
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.view_map_list_item, null));

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.title.setText(points.get(position).getName());
        holder.distance.setText(points.get(position).getDistance() + "m");
        holder.address.setText(points.get(position).getAddress());
            /*
            * 电话
            * */
        if (null != points.get(position).getAdditionalInformation()) {
            if (TextUtils.isEmpty(points.get(position).getAdditionalInformation().getTelephone())) {
                holder.call.setEnabled(false);
                holder.call.setTextColor(Color.GRAY);
            } else {
                holder.call.setTextColor(Color.WHITE);
                holder.call.setEnabled(true);
                holder.call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        callSupportPhone(points.get(position).getAdditionalInformation().getTelephone());
                    }
                });
            }
        }
    /*
    * 百度地图
    * */
        holder.location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double lat = points.get(position).getLocation().getLat();
                double lng = points.get(position).getLocation().getLng();
                String address = points.get(position).getAddress();
                String name = points.get(position).getName();
                int distance = points.get(position).getDistance();
                OPenMap(lng, lat, name, address, distance);
            }


        });


        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return points.size();
    }

    public void addItem(GetMapList.PointListEntity note) {

        points.add(note);
        notifyItemInserted(0);
    }

    public void removeItem(int position) {
        points.remove(position);
        notifyItemRemoved(position);
    }

    /*
   * 拨打客服号码
   * */
    protected void callSupportPhone(String phone) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phone));
            context.startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(context, "无发现拨号程序，请稍后再试！", Toast.LENGTH_SHORT).show();
        }

    }

    protected void OPenMap(double lng, double lat, String name, String address, int distance) {
        Intent intent = new Intent(context, MapActivity.class);
        intent.putExtra("aim_lng", lng);
        intent.putExtra("aim_lat", lat);
        intent.putExtra("name", name);
        intent.putExtra("address", address);
        intent.putExtra("distance", distance);
        intent.putExtra("keyWord", keyWord);
        context.startActivity(intent);

    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView distance;
        TextView call;
        TextView location;
        FousedTextView address;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.tv_map_title);
            distance = (TextView) view.findViewById(R.id.tv_map_distance);
            call = (TextView) view.findViewById(R.id.tv_map_call);
            location = (TextView) view.findViewById(R.id.tv_map_location);
            address = (FousedTextView) view.findViewById(R.id.tv_map_address);

        }
    }
}
